<?php

namespace App\Console;

use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();

        // 每小时执行一次的任务
        $schedule->call(function () {
            // 广告的自动上线或下线处理
            $now_date = Carbon::now();
            // 广告待上架
            DB::table('advertisements')->where('start_at', '>', $now_date)->where('enable', '<>', 'F')->update(['enable' => 'F']);
            // 广告过期处理
            DB::table('advertisements')->where('end_at', '<', $now_date)->where('enable', '<>', 'D')->update(['enable' => 'D']);

            // 设置的下架商品
            $diagnosis_at = DB::table('admin_params')->where('id', 1)->value('valid_day');
            // 如果鉴定过期时间不设置为 0 的话
            if($diagnosis_at != 0){
                $valid_timestamp = $diagnosis_at * 86400;       // 过期的时间：秒
                $comput_timestamp = (Carbon::now()->timestamp) - $valid_timestamp;     // 当前时间戳 - 过期时间
                $comput_at = Carbon::createFromTimestamp($comput_timestamp)->toDateTimeString();
                DB::table('cars')->where('diagnosis_at', '<', $comput_at)->update(['status' => '2']);
            }
        })->hourly();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
